package com.acheli.registry.network;

import com.acheli.ACHeli;
import com.acheli.registry.network.packet.*;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.network.NetworkRegistry;
import net.minecraftforge.network.PacketDistributor;
import net.minecraftforge.network.simple.SimpleChannel;

public class ACHMessages {
    public static SimpleChannel CHANNEL;

    private static int packetId = 0;
    public static int id() {
        return packetId ++;
    }

    public static void register() {
        SimpleChannel channel = NetworkRegistry.ChannelBuilder
                .named(ResourceLocation.tryBuild(ACHeli.MODID, "messages"))
                .networkProtocolVersion(() -> "1.0")
                .clientAcceptedVersions(s -> true)
                .serverAcceptedVersions(s -> true)
                .simpleChannel();

        CHANNEL = channel;
        //下面注册网络包
        ExampleC2SPacket.register(channel);
        CustomSyncableC2SPacket.register(channel);
        CustomSyncableS2CPacket.register(channel);
        ComponentSpawnC2SPacket.register(channel);
        ComponentSpawnS2CPacket.register(channel);
        ComponentSetupC2SPacket.register(channel);
        RideableSpawnS2CPacket.register(channel);
        VehiclePackRloadEventC2SPacket.register(channel);
        EntityJoinC2SPacket.register(channel);
        EntityLeaveC2SPacket.register(channel);
        GsonComponentFLRegistryC2SPacket.register(channel);
        GsonDefaultComponentReloadC2SPacket.register(channel);


    }

    public static <MSG> void  sendToServer (MSG message) {
        CHANNEL.sendToServer(message);

    }

    public static <MSG> void sendToPlayer(MSG message, ServerPlayer player) {
        CHANNEL.send(PacketDistributor.PLAYER.with(() -> player), message);

    }

}
